
from whiff.middleware import misc
from whiff.middleware import insertIfNeeded
from Chart import PieChart
from whiff import gateway
from whiff import whiffenv

class amChart(misc.utility):
    "virtual superclass"
    def __init__(self, **args):
        self.args = args
    def envPrefix(self):
        raise ValueError, "must be defined at subclass"
    def addParamFromArgOrEnv(self, todict, name, env, default=None, asJson=False):
        value = self.getParamFromArgOrEnv(name, env, default, asJson)
        #pr "addparam", (name, value)
        todict[name] = self.getParamFromArgOrEnv(name, env, default, asJson)
    def getParamFromArgOrEnv(self, name, env, default=None, asJson=False):
        if self.args.has_key(name):
            arg = self.args[name]
            if asJson:
                return self.param_json(arg, env)
            else:
                return self.param_value(arg, env)
        else:
            # look in env, or return default
            prefix = self.envPrefix()
            envName = prefix+name
            return env.get(envName, default)
    def getChart(self, env, start_response):
        raise ValueError, "define at subclass"
    def freshName(self, env):
        return gateway.getResource(env, ["freshName", "amchartsVariable"])
    def absolutePath(self, relativePath, env):
        return whiffenv.absPath(env, relativePath)
    def getswfFile(self, env):
        raise ValueError, "define at subclass"
    def getpath(self, env):
        raise ValueError, "define at subclass"
    def getswfObjectJs(self, env):
        raise ValueError, "define at subclass"
    def __call__(self, env, start_response):
        C = self.getChart(env, start_response)
        specs = {}
        self.addParamFromArgOrEnv(specs, "jsVariable", env, default=self.freshName(env))
        self.addParamFromArgOrEnv(specs, "flashId", env, default=self.freshName(env))
        self.addParamFromArgOrEnv(specs, "flashName", env, default=self.freshName(env))
        # maybe should require explicit width/height
        self.addParamFromArgOrEnv(specs, "width", env, default=500, asJson=True)
        self.addParamFromArgOrEnv(specs, "height", env, default=400, asJson=True)
        self.addParamFromArgOrEnv(specs, "swfFile", env, default=self.getswfFile(env))
        self.addParamFromArgOrEnv(specs, "path", env, default=self.getpath(env))
        self.addParamFromArgOrEnv(specs, "swfObjectJs", env, default=self.getswfObjectJs(env))
        self.addParamFromArgOrEnv(specs, "flashVersion", env)
        self.addParamFromArgOrEnv(specs, "backgroundColor", env)
        C.specifications(**specs)
        # insert the javascript libraries if not already included
        libInclude = C.swfObjectScript()
        libtextApp = insertIfNeeded.__middleware__(text=libInclude, doneFlag="amcharts.swfObjectJs")
        libtextOption = self.param_value(libtextApp, env)
        # get the chart js and html
        fragment = C.htmlFragment()
        if libtextOption:
            fragment = libtextOption + "\n" + fragment
        return self.deliver_page(fragment, env, start_response)

class amPie(amChart):
    def envPrefix(self):
        return "amchart." # xxxx??
    def getswfFile(self, env):
        return self.absolutePath("ampie/ampie.swf", env)
    def getpath(self, env):
        return self.absolutePath("ampie/", env)
    def getswfObjectJs(self, env):
        return self.absolutePath("ampie/swfobject.js", env)
    def getChart(self, env, start_response):
        C = PieChart()
        D = {}
        self.addParamFromArgOrEnv(D, "settings", env, {}, asJson=True)
        self.addParamFromArgOrEnv(D, "pie", env, [], asJson=True)
        self.addParamFromArgOrEnv(D, "labels", env, {}, asJson=True)
        C.fromDict(D, env)
        return C
    
__middleware__ = amPie
